﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Collections;
using System.IO;
using System.Web.Security;

public partial class account_settings : FBasePage
{
    // Fields

    private bool fileok = false;

    public bool result = false;
    public string strCurrency = "";
    public string strMoney = "";

    public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
  

    // Methods
    private void GetSystemInfo()
    {
        string str = "";
        DataSet list = new Maticsoft.BLL. System().GetList("");
        if (((list != null) && (list.Tables.Count > 0)) && (list.Tables[0].Rows.Count > 0))
        {
            str = list.Tables[0].Rows[0]["Currency"].ToString();
        }
    }

    private void initDropdown()
    {
        this.city.Items.Add(new ListItem("全部城市", "0"));
        DataTable cityList = base.CityList;
        if (cityList.Rows.Count > 0)
        {
            for (int i = 0; i < cityList.Rows.Count; i++)
            {
                this.city.Items.Add(new ListItem(cityList.Rows[i]["Name"].ToString(), cityList.Rows[i]["id"].ToString()));
            }
        }
    }

    private void initPage()
    {
        if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
        {
            base.Response.Redirect("Login.aspx");
        }
        string name = this.Page.User.Identity.Name;
        Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
        string strWhere = "";
        strWhere = string.Format(" Username='{0}'", name);
        DataSet list = info.GetList(strWhere);
        if (((list != null) && (list.Tables.Count > 0)) && (list.Tables[0].Rows.Count > 0))
        {
            this.emails.Value = list.Tables[0].Rows[0]["Email"].ToString();
            if ((list.Tables[0].Rows[0]["Avatar"] == null) || (list.Tables[0].Rows[0]["Avatar"].ToString() == ""))
            {
                this.avatar.Visible = false;
            }
            else
            {
                this.avatar.Src = list.Tables[0].Rows[0]["Avatar"].ToString();
            }
            this.username.Value = list.Tables[0].Rows[0]["Username"].ToString();
            this.hPwd.Value = list.Tables[0].Rows[0]["Password"].ToString();
            this.password.Value = list.Tables[0].Rows[0]["Password"].ToString();
            this.passwordconfirm.Value = list.Tables[0].Rows[0]["Password"].ToString();
            this.qq.Value = list.Tables[0].Rows[0]["Qq"].ToString();
            this.realname.Value = list.Tables[0].Rows[0]["Realname"].ToString();
            this.zipcode.Value = list.Tables[0].Rows[0]["Zipcode"].ToString();
            this.address.Value = list.Tables[0].Rows[0]["Address"].ToString();
            this.mobile.Value = list.Tables[0].Rows[0]["Mobile"].ToString();
            this.hID.Value = list.Tables[0].Rows[0]["id"].ToString();
            this.hfManger.Value = list.Tables[0].Rows[0]["Manager"].ToString();
            if ((list.Tables[0].Rows[0]["Money"] == null) || (list.Tables[0].Rows[0]["Money"].ToString() == ""))
            {
                this.strMoney = "0";
            }
            else
            {
                this.strMoney = list.Tables[0].Rows[0]["Money"].ToString();
            }
            if ((list.Tables[0].Rows[0]["Gender"] == null) || (list.Tables[0].Rows[0]["Gender"].ToString() == ""))
            {
                this.gender.Value = "M";
            }
            else
            {
                this.gender.Value = list.Tables[0].Rows[0]["Gender"].ToString();
            }
            if ((list.Tables[0].Rows[0]["City_id"] == null) || (list.Tables[0].Rows[0]["City_id"].ToString() == ""))
            {
                this.city.Value = "2";
            }
            else
            {
                this.city.Value = list.Tables[0].Rows[0]["City_id"].ToString();
            }
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.MyWebside1.Select = "settings";
        if (!this.Page.IsPostBack)
        {
            this.initDropdown();
            this.initPage();
            this.GetSystemInfo();
        }
    }

    protected void submit_ServerClick(object sender, EventArgs e)
    {
        if (!(this.Page.User.Identity.IsAuthenticated && !(this.Page.User.Identity.Name == "")))
        {
            base.Response.Redirect("Login.aspx");
        }
        Maticsoft.Model.UserInfo model = new Maticsoft.Model.UserInfo();
        model = this.userbll.GetModel(base.AsUser.Id);
        model.Username = this.username.Value;
        if (this.upload_image.PostedFile.ContentLength > 0x7d000)
        {
            this.SetError("请上传 512KB 以内的个人肖像图片!");
            base.Response.Redirect("settings.aspx");
        }
        string str = this.upload_image.Value;
        string str2 = "";
        string str3 = "";
        if (this.upload_image.Value != "")
        {
            int startIndex = str.LastIndexOf(".");
            str3 = str.Substring(startIndex);
            str2 = DateTime.Now.Ticks.ToString() + str3;
        }
        try
        {
            if (str != "")
            {
                string path = base.Server.MapPath("~/upfile/headphoto/");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                string[] strArray = new string[] { ".gif", ".png", ".bmp", ".jpg", ".jpeg" };
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (str3 == strArray[i])
                    {
                        this.fileok = true;
                    }
                }
                if (this.fileok)
                {
                    this.upload_image.PostedFile.SaveAs(path + str2);
                    this.avatar.Src = "/upfile/headphoto/" + str2;
                }
            }
            else
            {
                this.fileok = true;
            }
        }
        catch (Exception exception)
        {
            base.Response.Write(exception);
        }
        if (this.fileok)
        {
            model.Avatar = this.avatar.Src;
            if (this.password.Value == "")
            {
                model.Password = this.hPwd.Value;
            }
            else
            {
                model.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(this.password.Value + "@4!@#$%@", "md5");
            }
            model.Email = this.emails.Value;
            try
            {
                if (this.mobile.Value != "")
                {
                    model.Mobile = base.Server.HtmlEncode(Convert.ToUInt64(this.mobile.Value).ToString());
                }
            }
            catch (Exception)
            {
                this.result = true;
                this.SetError("您输入的手机号码格式不正确，修改失败");
                base.Response.Redirect("settings.aspx");
            }
            try
            {
                if (this.qq.Value != "")
                {
                    model.Qq = base.Server.HtmlEncode(Convert.ToUInt64(this.qq.Value).ToString());
                }
            }
            catch (Exception)
            {
                this.result = true;
                this.SetError("您输入的QQ号码格式不正确，修改失败");
                base.Response.Redirect("settings.aspx");
            }
            try
            {
                if (this.zipcode.Value != "")
                {
                    model.Zipcode = base.Server.HtmlEncode(Convert.ToUInt64(this.zipcode.Value).ToString());
                }
            }
            catch (Exception)
            {
                this.result = true;
                this.SetError("您输入的邮政编码格式不正确，修改失败");
                base.Response.Redirect("settings.aspx");
            }
            if (!this.result)
            {
                model.Realname = base.Server.HtmlEncode(this.realname.Value);
                model.Address = base.Server.HtmlEncode(this.address.Value);
                model.City_id = int.Parse(this.city.Value);
                model.Gender = this.gender.Value;
                model.Id = int.Parse(this.hID.Value);
                model.Manager = this.hfManger.Value;
                new Maticsoft.BLL.UserInfo().Update(model);
                base.SetSuccess("修改成功！");
                base.Response.Redirect("settings.aspx");
            }
        }
        else
        {
            this.SetError("友情提示：上传图片类型不合法");
            base.Response.Redirect("settings.aspx");
        }
    }

}